function DestroyUnitRunTriggersAndEffect takes unit unitToStop returns nothing
	local integer unitId = GetHandleId(unitToStop)
	local trigger stopTrigger = LoadTriggerHandle(udg_UnitDataHashTable , unitId , StringHash("ABILITY_RUN_STOP_TRIGGER"))
	local triggeraction stopTriggerAction = LoadTriggerActionHandle(udg_UnitDataHashTable , unitId , StringHash("ABILITY_RUN_STOP_TRIGGER_ACTION"))
	local trigger noManaTrigger = LoadTriggerHandle(udg_UnitDataHashTable , unitId , StringHash("ABILITY_RUN_NO_MANA_TRIGGER"))
	local triggeraction noManaTriggerAction = LoadTriggerActionHandle(udg_UnitDataHashTable , unitId , StringHash("ABILITY_RUN_NO_MANA_TRIGGER_ACTION"))
	//--
	call UnitRemoveAbility(unitToStop , 'A00D')
	call UnitRemoveBuffBJ('B001' , unitToStop)
	call TriggerRemoveAction(stopTrigger , stopTriggerAction)
	call DestroyTrigger(stopTrigger)
	call TriggerRemoveAction(noManaTrigger , noManaTriggerAction)
	call DestroyTrigger(noManaTrigger)
	call RemoveSavedHandle(udg_UnitDataHashTable , unitId , StringHash("ABILITY_RUN_STOP_TRIGGER"))
	call RemoveSavedHandle(udg_UnitDataHashTable , unitId , StringHash("ABILITY_RUN_STOP_TRIGGER_ACTION"))
	call RemoveSavedHandle(udg_UnitDataHashTable , unitId , StringHash("ABILITY_RUN_NO_MANA_TRIGGER"))
	call RemoveSavedHandle(udg_UnitDataHashTable , unitId , StringHash("ABILITY_RUN_NO_MANA_TRIGGER_ACTION"))
	//--
	set unitToStop = null
	set stopTrigger = null
	set stopTriggerAction = null
	set noManaTrigger = null
	set noManaTriggerAction = null
endfunction

function RemoveRunBuff takes nothing returns nothing
	local timer removeBuffTimer = GetExpiredTimer()
	local integer timerId = GetHandleId(removeBuffTimer)
	local unit unitToRemoveRunBuff = LoadUnitHandle(udg_HT , timerId , 3)
	//--
	if UnitHasBuffBJ(unitToRemoveRunBuff , 'B000') == false then
		call DestroyUnitRunTriggersAndEffect(unitToRemoveRunBuff)
	endif
	call PauseTimer(removeBuffTimer)
	call FlushChildHashtable(udg_HT , timerId)
	call DestroyTimer(removeBuffTimer)
	//--
	set removeBuffTimer = null
	set unitToRemoveRunBuff = null
endfunction

function StopRunning takes nothing returns nothing
	local timer removeBuffTimer = null
	local integer timerId = 0
	//--
	if GetIssuedOrderId() == OrderId("unimmolation") then		
		set removeBuffTimer = CreateTimer()
		set timerId = GetHandleId(removeBuffTimer)
		call SaveUnitHandle(udg_HT , timerId , 3 , GetTriggerUnit())
		call TimerStart(removeBuffTimer , 0.0 , false , function RemoveRunBuff)
	endif
	//--
	set removeBuffTimer = null
endfunction

function AddRun takes nothing returns nothing
	local timer theTimer = GetExpiredTimer()
	local unit unitToAddRun = LoadUnitHandle(udg_HT , GetHandleId(theTimer) , 3)
	//--
	call UnitAddAbility(unitToAddRun , 'A002')
	call PauseTimer(theTimer)
	call FlushChildHashtable(udg_HT , GetHandleId(theTimer))
	call DestroyTimer(theTimer)
	//--
	set theTimer = null
	set unitToAddRun = null
endfunction

function StopRunningNoMana takes nothing returns nothing
	local timer addRunTimer = CreateTimer()
	local unit noManaUnit = GetTriggerUnit()
	//--
	call DestroyUnitRunTriggersAndEffect(noManaUnit)
	call UnitRemoveAbility(noManaUnit , 'A002')
	call SaveUnitHandle(udg_HT , GetHandleId(addRunTimer) , 3 , noManaUnit)
	call TimerStart(addRunTimer, 0.0 , false , function AddRun)
	call DisplayTextToPlayer(GetOwningPlayer(noManaUnit) , 0 , 0 , "你跑不動了！")
	//--
	set addRunTimer = null
	set noManaUnit = null
endfunction


function RunAction takes nothing returns nothing
	local trigger stopTrigger
	local triggeraction stopTriggerAction
	local trigger noManaTrigger
	local triggeraction noManaTriggerAction
	local unit unitToRun = GetTriggerUnit()
	local integer unitId = GetHandleId(unitToRun)
	local timer addRunTimer
	local real manaLimit = 10.0
	//--
	if GetUnitState(GetTriggerUnit() , UNIT_STATE_MANA) >= manaLimit then
		call UnitAddAbility(unitToRun , 'A00D')
		//create stop running trigger
		set stopTrigger = CreateTrigger()
		call TriggerRegisterUnitEvent( stopTrigger , unitToRun , EVENT_UNIT_ISSUED_ORDER )
		set stopTriggerAction = TriggerAddAction(stopTrigger , function StopRunning)
		
		set noManaTrigger = CreateTrigger()
		call TriggerRegisterUnitManaEvent( noManaTrigger , unitToRun , LESS_THAN_OR_EQUAL , manaLimit )
		set noManaTriggerAction = TriggerAddAction(noManaTrigger , function StopRunningNoMana)
		
		call SaveTriggerHandle(udg_UnitDataHashTable , unitId , StringHash("ABILITY_RUN_STOP_TRIGGER") , stopTrigger)
		call SaveTriggerActionHandle(udg_UnitDataHashTable , unitId , StringHash("ABILITY_RUN_STOP_TRIGGER_ACTION") , stopTriggerAction)
		call SaveTriggerHandle(udg_UnitDataHashTable , unitId , StringHash("ABILITY_RUN_NO_MANA_TRIGGER") , noManaTrigger)
		call SaveTriggerActionHandle(udg_UnitDataHashTable , unitId , StringHash("ABILITY_RUN_NO_MANA_TRIGGER_ACTION") , noManaTriggerAction)
	else
		call UnitRemoveAbility(unitToRun , 'A002')
		set addRunTimer = CreateTimer()
		call SaveUnitHandle(udg_HT , GetHandleId(addRunTimer) , 3 , unitToRun)
		call TimerStart(addRunTimer, 0.0 , false , function AddRun)
		call DisplayTextToPlayer(GetOwningPlayer(unitToRun) , 0 , 0 , "沒力氣再跑了....")
	endif
	//--
	set stopTrigger = null
	set stopTriggerAction = null
	set noManaTrigger = null
	set noManaTriggerAction = null
	set unitToRun = null
	set addRunTimer = null
endfunction

function Trig_RunTry takes nothing returns nothing
	if GetIssuedOrderId() == OrderId("immolation") then
		call RunAction()
	endif
endfunction

function InitTrig_Run takes nothing returns nothing
	set gg_trg_Run = CreateTrigger()
	call TriggerRegisterPlayerForceUnitEvent(gg_trg_Run , EVENT_PLAYER_UNIT_ISSUED_ORDER)
	call TriggerAddAction(gg_trg_Run , function Trig_RunTry)
endfunction